#!/usr/bin/perl

use strict 'vars';
package main;

use Getopt::Std;
use Cwd;

use vars qw($opt_f $opt_u $opt_o $opt_n);

#get command line options
getopts('f:u:o:n:p:');
if ($opt_f eq "" || $opt_n eq "" || $opt_o eq "") {
    die "Usage: pl_update-i3-status.pl -f <conf_file> -o <output_prefix> -n <name of i3 process (i3d/i3_server> -u <username>\n";
}
if ($opt_u eq "") {
    $opt_u = "ucb_i3";
}

my %ip2name = (), my %name2ip = ();
&get_host_map;

open (HTML, ">$opt_o.html");
open (TXT, ">$opt_o.txt");

open (CONF_FILE, $opt_f) || die "Cannot open conf file\n";

my %hosts = ();
my $line, my @arr, my $cmd;
my %i3_count = (), my %init_count = ();
my %tcp_count = (), my %id = (); my %port = ();
my %uptime_hash = ();
while ($line = <CONF_FILE>) {
    chomp $line;
    @arr = split(/ /, $line);
    my $host = $arr[0]; my $port = $arr[1]; my $chord_port = $arr[2];
    $hosts{$arr[0]} = $host;
    $port{$arr[0]} = $port;
    $id{$arr[0]} = $arr[3];
    
    print "IP: $arr[0], Port: $arr[1]\n";
	
    # check i3 server and chord
    open PS, "ssh -o \"StrictHostKeyChecking no\" $opt_u\@$host \"ps ax\"|";
    my $got_uptime = 0;
    while (<PS>) {
	if ($_ =~ /$opt_n/) {
	    $i3_count{$host}++;
	} elsif ($_ =~ /initiator/ && $_ =~ /$chord_port/) {
	    $init_count{$host}++;
	} elsif ($_ =~ /tcp_receiver/ && $_ =~ /$port/) {
	    $tcp_count{$host}++;
	}
	
	if ($got_uptime == 0) {
	    my $i; my $status = 0;
	    open UP, "ssh -o \"StrictHostKeyChecking no\" $opt_u\@$host \"uptime\"|";
	    my $uptime = <UP>;
	    my @spl_up_arr = split(/,/, $uptime);
	    my @up_spl_up_arr = split(/up/, $spl_up_arr[0]);
	    $uptime_hash{$host} = "$up_spl_up_arr[1]";
	    for ($i = 1; $i < int(scalar(@spl_up_arr)); $i++) {
		if ($spl_up_arr[$i] =~ /users/) {
		    last;
		}
		$uptime_hash{$host} = $uptime_hash{$host}."$spl_up_arr[$i]";
	    }
	    close UP;
	}
	$got_uptime = 1;
    }
    close PS;
}

close CONF_DIR;

# spew out details to html document
print HTML "<html>\n<head><title>i3 status</title>\n</head>\n";
print HTML "<body  bgcolor=\"#ffffff\" text=\"#000000\" link=\"#0000cc\" vlink=\"#cc00cc\" alink=\"#cc0000\"><center>\n";
print HTML "<h1><i>i3</i> status on PlanetLab</h1>\n";
print HTML "Last updated: ".localtime()." PST\n";

print HTML "<p>\n<center><table BORDER>\n";

print HTML "<tr BGCOLOR=\"#FFFFFF\" NOSAVE>\n<td><font FACE=\"Times New Roman,Times\">";
print HTML "<b>Host name</b>";
print HTML "</font></td>\n<td><font FACE=\"Times New Roman,Times\">Uptime";
print HTML "</font></td>\n<td><font FACE=\"Times New Roman,Times\">IP:Port";
print HTML "</font></td>\n<td><font FACE=\"Times New Roman,Times\">Chord ID";
print HTML "</font></td>\n<td><font FACE=\"Times New Roman,Times\">i3_server/chord";
#print HTML "</font></td>\n<td><font FACE=\"Times New Roman,Times\">news_validator";
#print HTML "</font></td>\n<td><font FACE=\"Times New Roman,Times\">news_tcp_recvr";
print HTML "</font></td>\n</tr>\n";

my $host; my $bgcolor, my $str;
foreach $host (sort keys %hosts) {
    print HTML "<td><font FACE=\"Times New Roman,Times\">";
    print HTML "$ip2name{$host}";
    print TXT "$ip2name{$host} ";

    print HTML "</font></td>\n<td><font FACE=\"Times New Roman,Times\">";
    print HTML "$uptime_hash{$host}";
    
    print HTML "</font></td>\n<td><font FACE=\"Times New Roman,Times\">";
    print HTML "$host".":$port{$host}";
    print TXT "$host".":$port{$host} ";
    
    print HTML "</font></td>\n<td><font FACE=\"Times New Roman,Times\">";

    my $id_host = sprintf "%s", $id{$host};
    print HTML "$id_host";
    print TXT "$id_host ";
    
    if ($i3_count{$host} == 0) {
	$bgcolor = "red";
	$str = "No";
    } elsif ($i3_count{$host} == 1) {
	$bgcolor = "orange";
	$str = "Partial";
    } elsif ($i3_count{$host} == 2) {
	$bgcolor = "white";
	$str = "Running";
    } else {
	$bgcolor = "blue";
	$str = "Invalid";
	$str = $i3_count{$host};
    }
    print HTML "</font></td>\n<td BGCOLOR=$bgcolor><font FACE=\"Times New Roman,Times\">";
    print HTML $str;
    print TXT $str."\n";

    if ($init_count{$host} == 0) {
	$bgcolor = "red";
	$str = "No";
    } elsif ($init_count{$host} == 1) {
	$bgcolor = "white";
	$str = "Running";
    } else {
	$bgcolor = "blue";
	$str = "Invalid$init_count{$host}";
    }
    # print HTML "</font></td>\n<td BGCOLOR=$bgcolor><font FACE=\"Times New Roman,Times\">";
    # print HTML $str;

    if ($tcp_count{$host} == 0) {
	$bgcolor = "red";
	$str = "No";
    } elsif ($tcp_count{$host} == 1) {
	$bgcolor = "white";
	$str = "Running";
    } else {
	$bgcolor = "blue";
	$str = "Invalid";
	$str = $tcp_count{$host};
    }
    # print HTML "</font></td>\n<td BGCOLOR=$bgcolor><font FACE=\"Times New Roman,Times\">";
    # print HTML $str;
    print HTML "</font></td>\n</tr>\n";
}
print HTML "</table></center>\n";

close HTML;
close TXT;

sub get_host_map {
    open HOSTS, "hosts.txt";
    while (<HOSTS>) {
	chomp $_;
	my @opts = split / /, $_;
	$ip2name{$opts[1]} = $opts[0];
	$name2ip{$opts[0]} = $opts[1];
    }
    close HOSTS;
}
